//
//  ZCMechanismListVC.m
//  zctx
//
//  Created by 郭亚京 on 15/10/9.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCMechanismListVC.h"
#import "ZCMechanismCell.h"

@interface ZCMechanismListVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

//data

@property (nonatomic, strong) NSArray *arrMainList;

@end

@implementation ZCMechanismListVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCMechanismListVC"];
    [MTA trackPageViewBegin:@"ZCMechanismListVC"];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCMechanismListVC"];
    [MTA trackPageViewEnd:@"ZCMechanismListVC"];
}

-(void)updateRequest{
    if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus >0) {
        self.bannerView.hidden = YES;
    }
    [self requestMainList];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //AF 开启监听网络状态
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    [self setNavigationItems];
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"#ebeded"];
    self.navigationItem.title = @"选择检测机构";
    
    //添加tableview
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0,0));
    }];
    
    [self requestMainList];
}

#pragma mark - request

- (void)requestMainList {
    [NetworkManager requestFoundingGetLabssuccess:^(NSArray *array) {
        self.arrMainList = array;
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == 0) {
            self.bannerView.hidden = NO;
        }
    }];
}

#pragma mark - button

-(void)setNavigationItems {
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

- (void)backButtonPressed{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableView DataSource & Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.arrMainList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIndentity = @"ZCMechanismCell";
    ZCMechanismCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentity];
    if (!cell) {
        cell = [[ZCMechanismCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentity];
    }
    ZCMechanismModel *model = self.arrMainList[indexPath.section];
    
    [cell.imageViewIcon sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[NetworkManager strURLBasic], model.logo]]];
    [cell.labelName setText:model.name];
    [cell.labelContent setText:model.content];
   
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ZCMechanismModel *model = self.arrMainList[indexPath.section];
    self.cellselectMechanismList(model.id,model.name);
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - instance

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
    }
    return _tableView;
}

@end
